package com.mentalhealthplatform.teacherV2.controller;

import com.mentalhealthplatform.appModels.Tweet;
import com.mentalhealthplatform.teacherV2.service.TweetServiceV2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v2/tweets")
public class TweetControllerV2 {
    @Autowired
    private TweetServiceV2 tweetServiceV2;

    // 创建推文
    @PostMapping
    public ResponseEntity<Tweet> createTweet(@RequestBody Tweet tweet) {
        try {
            tweet.setCreatedAt(LocalDateTime.now());
            Tweet savedTweet = tweetServiceV2.saveTweet(tweet);
            return ResponseEntity.status(201).body(savedTweet);
        } catch (Exception e) {
            return ResponseEntity.status(500).body(null);
        }
    }

    // 更新推文
    @PutMapping("/{id}")
    public ResponseEntity<Tweet> updateTweet(@PathVariable Long id, @RequestBody Tweet tweet) {
        try {
            tweet.setId(id);
            Tweet updatedTweet = tweetServiceV2.saveTweet(tweet);
            return ResponseEntity.ok(updatedTweet);
        } catch (Exception e) {
            return ResponseEntity.status(500).body(null);
        }
    }

    // 发布推文
    @PutMapping("/{id}/restore")
    public ResponseEntity<Tweet> restoreTweet(@PathVariable Long id) {
        try {
            Tweet tweet = tweetServiceV2.getTweetById(id);
            if (tweet == null) {
                return ResponseEntity.notFound().build();
            }
            // 将 "active" 改为 "draft"
            tweet.setStatus("draft");
            Tweet restoredTweet = tweetServiceV2.saveTweet(tweet);
            return ResponseEntity.ok(restoredTweet);
        } catch (Exception e) {
            return ResponseEntity.status(500).body(null);
        }
    }

    // 上传文件
    @PostMapping("/upload")
    public ResponseEntity<Map<String, String>> uploadFile(@RequestParam("file") MultipartFile file) {
        String fileUrl = "http://localhost:8080/uploads/" + file.getOriginalFilename();
        Map<String, String> response = new HashMap<>();
        response.put("url", fileUrl);
        return ResponseEntity.ok(response);
    }

    // 获取所有推文
    @GetMapping
    public ResponseEntity<List<Tweet>> getAllTweets() {
        List<Tweet> tweets = tweetServiceV2.getAllTweets();
        return ResponseEntity.ok(tweets);
    }

    // 获取草稿箱推文
    @GetMapping("/drafts")
    public ResponseEntity<List<Tweet>> getDrafts() {
        List<Tweet> drafts = tweetServiceV2.getDrafts();
        return ResponseEntity.ok(drafts);
    }

    // 获取回收站推文
    @GetMapping("/trash")
    public ResponseEntity<List<Tweet>> getTrash() {
        List<Tweet> trash = tweetServiceV2.getTrash();
        return ResponseEntity.ok(trash);
    }

    // 获取指定推文
    @GetMapping("/{id}")
    public ResponseEntity<Tweet> getTweetById(@PathVariable Long id) {
        Tweet tweet = tweetServiceV2.getTweetById(id);
        return tweet != null ? ResponseEntity.ok(tweet) : ResponseEntity.notFound().build();
    }

    // 删除推文
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteTweet(@PathVariable Long id) {
        tweetServiceV2.moveToTrash(id);
        return ResponseEntity.noContent().build();
    }

    // 永久删除推文
    @DeleteMapping("/permanent/{id}")
    public ResponseEntity<Void> permanentlyDeleteTweet(@PathVariable Long id) {
        tweetServiceV2.deleteTweet(id);
        return ResponseEntity.noContent().build();
    }
}